package com.springboot.service.impl;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.springboot.mapper.DeliveryAddressMapper;
import com.springboot.model.po.DeliveryAddress;
import com.springboot.service.DeliveryAddressService;

@Service
public class DeliveryAddressServiceImpl implements DeliveryAddressService {
	@Autowired
	DeliveryAddressMapper deliveryAddressMapper;

	@Override
	public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) {
		List<DeliveryAddress> list = deliveryAddressMapper.listDeliveryAddressByUserId(userId);
		Iterator<DeliveryAddress> iterator = list.iterator();
		while (iterator.hasNext()) {
			DeliveryAddress da = iterator.next();
			if (da.getRemark().equals("1")) {
				iterator.remove(); // 使用迭代器安全地移除元素
			}
		}
		return list;
	}

	@Override
	public DeliveryAddress getDeliveryAddressById(Integer daId) {
		DeliveryAddress deliveryAddress = deliveryAddressMapper.getDeliveryAddressById(daId);
//		if(deliveryAddress == null) {
//			deliveryAddress = new DeliveryAddress();
//			return deliveryAddress;
//		}else if(deliveryAddress.getRemark().equals("1")) {
//			deliveryAddress = new DeliveryAddress();
//			return deliveryAddress;
//		}
		return deliveryAddress;
	}

	@Override
	public int saveDeliveryAddress(DeliveryAddress deliveryAddress) {
		// 判断新的和旧的是否一致
		for (DeliveryAddress da : deliveryAddressMapper.listDeliveryAddressByUserId(deliveryAddress.getUserId())) {
			// {contactName},#{contactSex},#{contactTel},#{address},#{userId},daId
			if (da.getAddress().equals(deliveryAddress.getAddress())
					&& da.getContactName().equals(deliveryAddress.getContactName())
					&& da.getContactSex().equals(deliveryAddress.getContactSex())
					&& da.getContactTel().equals(deliveryAddress.getContactTel())
					&& da.getUserId().equals(deliveryAddress.getUserId()))
				return 0;
		}
		return deliveryAddressMapper.saveDeliveryAddress(deliveryAddress);
	}

	@Transactional
	@Override
	public int updateDeliveryAddress(DeliveryAddress deliveryAddress) { // 存储一个新的，将老的设置为标签1（已删除）
		// 判断新的和旧的是否一致
		for (DeliveryAddress da : deliveryAddressMapper.listDeliveryAddressByUserId(deliveryAddress.getUserId())) {
			// {contactName},#{contactSex},#{contactTel},#{address},#{userId},daId
			if (da.getAddress().equals(deliveryAddress.getAddress())
					&& da.getContactName().equals(deliveryAddress.getContactName())
					&& da.getContactSex().equals(deliveryAddress.getContactSex())
					&& da.getContactTel().equals(deliveryAddress.getContactTel())
					&& da.getUserId().equals(deliveryAddress.getUserId()))
				return 0;
		}
		deliveryAddressMapper.saveDeliveryAddress(deliveryAddress);
		return deliveryAddressMapper.updateDeliveryAddress(deliveryAddress);
	}

	@Override
	public int removeDeliveryAddress(Integer daId) {
		DeliveryAddress deliveryAddress = deliveryAddressMapper.getDeliveryAddressById(daId);
		if (deliveryAddress != null) {
			// 判断id合法
			String remark = deliveryAddress.getRemark();
			if (remark.equals("1"))
				// 删除过的不能再删除
				return 0;
		}
		return deliveryAddressMapper.updateDeliveryAddress(deliveryAddress);
	}

}
